package com.ytd.smartcanteen.weighting.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ytd.common.util.DeviceUtil;
import com.ytd.smartcanteen.weighting.MainActivity;
import com.ytd.smartcanteen.weighting.devices.ScaleManager;

import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;

import cn.ytd.smartcanteen.weighting.R;

public class ScaleDialog extends BaseDialog {
    MainActivity mActivity;
    Button btnBack, btnZeroing;

    private final TextView tv_show_weight;
    private final TextView tv_show_peel_weight;
    private final View mMenuView;

    @SuppressLint({"InflateParams", "SetTextI18n", "UseCompatLoadingForDrawables"})
    public ScaleDialog(MainActivity context) {
        super(context);
        mActivity = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_scale, null);
        mMenuView.findViewById(R.id.ll_show_device).setOnLongClickListener(v -> {
            Toast.makeText(mMenuView.getContext(), DeviceUtil.getDeviceSN(), Toast.LENGTH_LONG).show();
            return false;
        });
        btnZeroing = mMenuView.findViewById(R.id.btn_scale_zeroing);
        btnZeroing.setOnClickListener(v -> ScaleManager.getInstance().zeroing());
        mMenuView.findViewById(R.id.btn_scale_remove_peel).setOnClickListener(v -> ScaleManager.getInstance().setPeel());
        tv_show_weight = mMenuView.findViewById(R.id.tv_show_weight);
        tv_show_weight.setText(ScaleManager.getInstance().getStableWeight().doubleValue() + "kg");

        tv_show_peel_weight = mMenuView.findViewById(R.id.tv_show_peel_weight);
        tv_show_peel_weight.setText(ScaleManager.getInstance().getPeelWeight().doubleValue() + "kg");

        btnBack = mMenuView.findViewById(R.id.set_btn_back);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();

        btnBack.setOnClickListener(v -> dismiss());
        this.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
        this.setContentView(mMenuView);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(h - 50);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        this.setAnimationStyle(R.style.DialogShow);
    }

    public void show(int gravity, int x, int y) {
        showAtLocation(mMenuView, gravity, x, y);
    }


    @SuppressLint("SetTextI18n")
    public void onScaleChange(@Nullable BigDecimal decimal) {
        if (decimal == null || tv_show_weight == null) {
            return;
        }
        tv_show_weight.setText(decimal.doubleValue() + "kg");
        tv_show_peel_weight.setText(ScaleManager.getInstance().getPeelWeight().doubleValue() + "kg");
    }
}
